package com.timguan.render.web.common;

import com.timguan.render.service.common.B2CMallServiceConfig;
import net.pocrd.entity.ServiceException;
import net.pocrd.entity.ServiceRuntimeException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by guankaiqiang on 2014/12/29.
 */
public class DefaultServiceErrorListener extends ServiceExceptionListener {
    private static final Logger logger = LoggerFactory.getLogger(DefaultServiceErrorListener.class);

    @Override
    public void initAcceptCodes() {

    }

    @Override
    public ModelAndView serviceExceptionEventHandle(int event, ServiceException se, ServiceRuntimeException sre, HttpServletRequest request, HttpServletResponse response) {
        try {
            if (!response.isCommitted()) {
                response.sendRedirect(B2CMallServiceConfig.getInstance().getPageNotFoundSite());
            } else {
                logger.warn("response has been commited!");
            }
        } catch (IOException e) {
            logger.error("redirect failed!", e);
        }
        return null;
    }
}
